package com.example.prison.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.prison.config.LoginInterceptor;
import com.example.prison.entity.CarManagement;
import com.example.prison.entity.VisitorAppointment;
import com.example.prison.mapper.CarManagementMapper;
import com.example.prison.service.VisitorAppointmentService;
import com.example.prison.mapper.VisitorAppointmentMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
* @author jiang
* @description 针对表【visitor_appointment】的数据库操作Service实现
* @createDate 2024-06-03 09:12:46
*/
@Service
public class VisitorAppointmentServiceImpl extends ServiceImpl<VisitorAppointmentMapper, VisitorAppointment>
    implements VisitorAppointmentService{

    @Autowired
    private VisitorAppointmentMapper visitorAppointmentMapper;

    @Autowired
    private CarManagementMapper carManagementMapper;

    @Override
    public PageInfo<VisitorAppointment> get(int pageNumber, int pageSize,String visitorName,String beginTime,String endTime,String appointmentStatus,String visitorStatus,String visitDepartment) {
        PageHelper.startPage(pageNumber, pageSize);
        String time = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        List<VisitorAppointment> list = visitorAppointmentMapper.get(visitorName, beginTime, endTime, appointmentStatus, visitorStatus, visitDepartment);
        for (VisitorAppointment appoint : list) {
            if(time.compareTo(appoint.getAppointmentTime()) > 0 && (Objects.equals(appoint.getAppointmentStatus(), "0") || Objects.equals(appoint.getAppointmentStatus(), "-1"))) {
                appoint.setAppointmentStatus("2");
                visitorAppointmentMapper.updateVisitorAppointment(appoint, appoint.getId());
            }
        }
//        //非超级管理员权限
//        if(!Objects.equals(LoginInterceptor.loginUser.getRoleName(), "超级管理员")) {
//            PageHelper.startPage(pageNumber, pageSize);
//            return new PageInfo<>(visitorAppointmentMapper.get(visitorName, beginTime, endTime, appointmentStatus, visitorStatus, LoginInterceptor.loginUser.getDepartment()));
//        }
        return new PageInfo<>(list);
    }

    @Override
    public int add(VisitorAppointment visitor) {
//        //非超级管理员权限
//        if(!Objects.equals(LoginInterceptor.loginUser.getRoleName(), "超级管理员") && !Objects.equals(LoginInterceptor.loginUser.getDepartment(), visitor.getVisitDepartment())) {
//            return 0;
//        }
        visitor.setAppointmentStatus("1");
        visitor.setVisitorStatus("0");
        List<VisitorAppointment> visitorNameList = visitorAppointmentMapper.getByName(visitor.getVisitorName());
        for(VisitorAppointment visitorOneName : visitorNameList) {
            if(ObjectUtils.isNotEmpty(visitorOneName)) {
                int timeResult = visitor.getAppointmentTime().compareTo(visitorOneName.getAppointmentTime());
                if(timeResult == 0 && Objects.equals(visitor.getAppointmentStatus(), "1")) {
                    return -1;
                }
            }
        }
        visitorAppointmentMapper.add(visitor);
        //访客信息若带有车辆，则将车辆加入车辆管理中
        if(ObjectUtils.isNotEmpty(visitor.getCarLicense())) {
            String[] carList= visitor.getCarLicense().split(",");
            for(String license : carList) {
                if(carManagementMapper.getListOneByNumber(license).size() < 1) {
                    CarManagement car = new CarManagement();
                    car.setCarStatus("营区外");
                    car.setCarNumber(license);
                    car.setCarDepart("");
                    carManagementMapper.add(car);
                }
            }
        }
        return 1;
    }

    @Override
    public void delete(Integer[] ids) {
        visitorAppointmentMapper.deleteVisitorAppointment(ids);
    }

    @Override
    public int update(VisitorAppointment visitor, Integer id) {
        visitorAppointmentMapper.updateVisitorAppointment(visitor, id);
        return 1;
    }

    @Override
    public VisitorAppointment getById(Integer id) {
        return visitorAppointmentMapper.getById(id);
    }

    @Override
    public VisitorAppointment getByIDCardNumber(String idCard){
        return visitorAppointmentMapper.getByIdCard(idCard);
    }

    @Override
    public String getCarNumberSum(String appointmentStatus) {
        return visitorAppointmentMapper.getCarNumberSum(appointmentStatus);
    }

    @Override
    public PageInfo<VisitorAppointment> getTodayVisitor(int pageNumber, int pageSize, String visitorName) {
        String time = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        PageHelper.startPage(pageNumber, pageSize);
        return new PageInfo<>(visitorAppointmentMapper.getTodayVisitor(time, visitorName));
    }

    @Override
    public String getTodayVisitorCarSum() {
        String time = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        return visitorAppointmentMapper.getTodayVisitorCarSum(time);
    }

    @Override
    public PageInfo<VisitorAppointment> getTodayVisitorCar(String carLicense) {
        String time = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        return new PageInfo<>(visitorAppointmentMapper.getTodayVisitorCar(time, carLicense));
    }

    @Override
    public PageInfo<VisitorAppointment> getRetentionVisitor(int pageNumber, int pageSize, String visitorStatus, String appointmentTime, String visitorName, String beginTime, String endTime) {
        return new PageInfo<>(visitorAppointmentMapper.getRetentionVisitor(visitorStatus, appointmentTime, visitorName, beginTime, endTime));
    }

    @Override
    public PageInfo<VisitorAppointment> getByStatusAndTime(int pageNumber, int pageSize,String visitorStatus, String appointmentTime, String visitorName, String beginTime, String endTime) {
        return new PageInfo<>(visitorAppointmentMapper.getByStatusAndTime(visitorStatus, appointmentTime, visitorName, beginTime, endTime));
    }

    @Override
    public List<VisitorAppointment> getByNameAndTimeOrderTime(String visitorName, String appointmentTime) {
        return visitorAppointmentMapper.getByNameAndTimeOrderTime(visitorName,appointmentTime);
    }

    @Override
    public VisitorAppointment getByPIDAndTime(String pid, String appointmentTime) {
        return visitorAppointmentMapper.getByPIDAndTime(pid,appointmentTime);
    }


}




